//
//  CTopBar.m
//  CWeekly
//
//  Created by  on 12-4-12.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "CTopBar.h"

@implementation CTopBar
@synthesize delegate,titleLabel,helpItem,searchItem,loginItem;
- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:LOGINED object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:SIGN_UP object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:RESIGN object:nil];
    [helpItem release];
    [searchItem release];
    [loginItem release];
    [titleLabel release];
    [barItems release];
    [super dealloc];
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loginEvent:) name:LOGINED object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(signupEvent:) name:SIGN_UP object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(resignEvent:) name:RESIGN object:nil];

        self.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"topbar.png"]];
        barItems=[[NSMutableArray alloc] init];
        UIImageView* logoBtn=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"logo.png"]];
        logoBtn.frame=CGRectMake(0, 1, 130, 40);
        UIView*v=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 130, 55)];
        v.backgroundColor=[UIColor clearColor];
        [v addSubview:logoBtn];
        [logoBtn release];
        UIBarButtonItem* barItem=[[UIBarButtonItem alloc] initWithCustomView:v];
        [barItems addObject:barItem];
        [v release];
        [barItem release];

        [self addSpaceItemCount:4];

        UIButton* loginBtn=[UIButton buttonWithType:UIButtonTypeCustom];
        [loginBtn setImage:[UIImage imageNamed:@"users.png"] forState:UIControlStateNormal];
        [loginBtn setImage:[UIImage imageNamed:@"users_s.png"] forState:UIControlStateSelected];
        loginBtn.tag=100;
        [loginBtn addTarget:self action:@selector(btnSelected:) forControlEvents:UIControlEventTouchUpInside];
        loginBtn.frame=CGRectMake(0, 0, 75, 35);
        loginItem=[[UIBarButtonItem alloc] initWithCustomView:loginBtn];
        [barItems addObject:loginItem];

        UIButton* searchBtn=[UIButton buttonWithType:UIButtonTypeCustom];
        [searchBtn setImage:[UIImage imageNamed:@"search.png"] forState:UIControlStateNormal];
        [searchBtn setImage:[UIImage imageNamed:@"search_s.png"] forState:UIControlStateSelected];
        searchBtn.frame=CGRectMake(0, 0, 75, 35);
        searchBtn.tag=101;
        [searchBtn addTarget:self action:@selector(btnSelected:) forControlEvents:UIControlEventTouchUpInside];
        searchItem=[[UIBarButtonItem alloc] initWithCustomView:searchBtn];
        [barItems addObject:searchItem];

        UIButton* helpBtn=[UIButton buttonWithType:UIButtonTypeCustom];
        [helpBtn setImage:[UIImage imageNamed:@"help.png"] forState:UIControlStateNormal];
        [helpBtn setImage:[UIImage imageNamed:@"help_s.png"] forState:UIControlStateSelected];
        helpBtn.frame=CGRectMake(0, 0, 75, 35);
        helpBtn.tag=102;
        [helpBtn addTarget:self action:@selector(btnSelected:) forControlEvents:UIControlEventTouchUpInside];

        helpItem=[[UIBarButtonItem alloc] initWithCustomView:helpBtn];
        [barItems addObject:helpItem];

        self.items=barItems;
    }
    return self;
}

-(void)addSpaceItemCount:(NSUInteger)count{
    UIBarButtonItem*  spaceBTN=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    
    for (int i=0; i<count; i++) {
        [barItems addObject:spaceBTN];
    }
    [spaceBTN release];
    
}

-(void)loginEvent:(NSNotification*)notify{
//    NSDictionary* dic=[notify object];
//    UIButton* la=[UIButton buttonWithType:UIButtonTypeCustom];
//    la.frame=CGRectMake(0, 0, 75, 35);
//    la.backgroundColor=[UIColor clearColor];
//    la.tag=100;
//    la.selected=YES;
//    [la addTarget:self action:@selector(btnSelected:) forControlEvents:UIControlEventTouchUpInside];
//    NSString* uid=[NSString stringWithFormat:@"用户:%@",[dic valueForKey:@"userId"]];
//    [la setTitle:uid forState:UIControlStateNormal];
//    [la setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
//    [la setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//
//    la.titleLabel.adjustsFontSizeToFitWidth=YES;
    
//    loginItem.customView=la;
    UIButton* logBtn=(UIButton* )loginItem.customView;
    [logBtn setImage:[UIImage imageNamed:@"logined.png"] forState:UIControlStateNormal];
    [logBtn setImage:[UIImage imageNamed:@"logined_s.png"] forState:UIControlStateSelected];
}
-(void)resignEvent:(NSNotification*)notify{
    UIButton* logBtn=(UIButton* )loginItem.customView;
    [logBtn setImage:[UIImage imageNamed:@"users.png"] forState:UIControlStateNormal];
    [logBtn setImage:[UIImage imageNamed:@"users_s.png"] forState:UIControlStateSelected];

}

-(void)signupEvent:(NSNotification*)notify{

}


-(void)btnSelected:(UIButton*)sender{
    [delegate topBar:self DidSelectButton:sender AtIndex:sender.tag];
    if (sender.selected) {
        sender.selected=NO;
    }else {
        for (UIBarButtonItem*item in barItems) {
            UIButton* btn=(UIButton*) item.customView;
            if (btn.tag>99) {
                if (btn.tag==sender.tag) {
                    btn.selected=YES;
                }else {
                    btn.selected=NO;
                }
            }
        }  
    }


}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
//    UIImage* img;
//    if (rect.size.width>1000) {
//        img=[UIImage imageNamed:@"topbar.png"];
//    }else {
//        img=[UIImage imageNamed:@"topbar_v.png"];
//    }
//    [img drawInRect:rect];
}


@end
